<?php
class Auth{
	public static $table = '';
	public static $idField = '';
	public static $nameField = '';
	public static $passwordField = '';
	public static $emailField = null;
	public static $userTypeField = null;
	
	public static function getTable(){
		return self::$table;
	}
	
	public static function setTable($table){
		self::$table = $table;
	}
	
	public static function setFields($id, $name, $password, $email = null, $type = null){
		self::$idField = $id;
		self::$nameField = $name;
		self::$passwordField = $password;
		self::$emailField = $email;
		self::$userTypeField = $type;
	}
	
	public static function attempt($username, $password){
		//TODO if successfull, save info in session.
	}
	
	public static function logout(){
		//TODO
		unset($_SESSION['userid']);
		unset($_SESSION['username']);
		unset($_SESSION['usertype']);
	}
	
	public static function check($type = null){
		if($type == null) return isset($_SESSION['userid']);
		if(self::$userTypeField == null)throw new ErrorException('[Auth] user type field is not set.');
		return isset($_SESSION['userid']) && isset($_SESSION['usertype']) && $_SESSION['usertype'] == $type;
	}
	
	public static function userType(){
		if(self::$userTypeField == null)throw new ErrorException('[Auth] user type field is not set.');
		return (isset($_SESSION['usertype']) ? $type : null);
	}
	
}
?>